package utils

import (
	"context"
	"github.com/google/uuid"
	"strings"
)

const (
	trackKey string = "trackID"
)

func getTraceId() string {
	id := uuid.New().String()
	return strings.Replace(id, "-", "", -1)
}

func TraceContext(ctx context.Context) context.Context {
	if _, ok := ctx.Value(trackKey).(string); ok {
		return ctx
	} else {
		return context.WithValue(ctx, trackKey, getTraceId())
	}
}

func GetTraceID(ctx context.Context) (context.Context, string) {
	if traceID, ok := ctx.Value(trackKey).(string); ok {
		return ctx, traceID
	} else {
		traceID := getTraceId()
		return context.WithValue(ctx, trackKey, traceID), traceID
	}
}
